package com.example.crm01.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.crm01.common.Result;
import com.example.crm01.entity.Business;
import com.example.crm01.page.TableDataInfo;
import com.example.crm01.service.BusinessService;
import com.example.crm01.service.form.business.BusinessAddForm;
import com.example.crm01.service.form.business.BusinessListForm;
import com.example.crm01.service.form.BaseIdForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Api(tags = "业务功能")
@RestController
@RequestMapping("/business")
@Slf4j
public class BusinessController {


    @Autowired
    private BusinessService businessService;

    @ApiOperation("业务列表")
    @PostMapping(value = "/list")
    public TableDataInfo list(@RequestBody BusinessListForm form) {
        TableDataInfo info = new TableDataInfo();
        log.info("打印入参的参数，form：{}", form);
        Page<Business> businessPageInfo = businessService.businessList(form);
        info.setCode(200);
        info.setMsg("请求成功");
        info.setRows(businessPageInfo.getRecords());
        info.setTotal(businessPageInfo.getTotal());
        return info;
    }


    @ApiOperation("业务录入")
    @PostMapping(value = "/add")
    public Result<Boolean> add(@RequestBody @Validated BusinessAddForm form) {
        return Result.success(businessService.insertBusiness(form));
    }

    @ApiOperation("业务修改")
    @PostMapping(value = "/update")
    public Result<Boolean> update(@RequestBody @Validated BusinessAddForm form) {
        return Result.success(businessService.updateBusiness(form));
    }


    @ApiOperation("业务删除")
    @PostMapping(value = "/delete")
    public Result<Boolean> delete(@RequestBody @Validated BaseIdForm form) {
        return Result.success(businessService.deleteBusiness(form));
    }

    @ApiOperation("业务详情")
    @PostMapping(value = "/details")
    public Result<Business> details(@RequestBody @Validated BaseIdForm form) {
        return Result.success(businessService.detailsBusiness(form));
    }


}
